package com.gurun.camera.device.util;

import com.danale.sdk.platform.entity.cloud.UserCloudInfo;
import com.gurun.camera.model.CloudDetailState;
import com.gurun.camera.model.DeviceCloudInfo;

import java.util.Comparator;

public class CloudComparator implements Comparator<DeviceCloudInfo> {

    @Override
    public int compare(DeviceCloudInfo lhs, DeviceCloudInfo rhs) {
        int cloudCompareResult = compareCloudState(lhs.getCloudState(),rhs.getCloudState());
        if(cloudCompareResult == 0){
                int cloudInfoCompareResult = compareCloudInfo(lhs.getCloudInfo(), rhs.getCloudInfo());
                return cloudInfoCompareResult;
        }else{
            return cloudCompareResult;
        }
    }

    private int compareCloudState(CloudDetailState lcloudState, CloudDetailState rcloudState){
        if(lcloudState != null && rcloudState != null){
            return lcloudState.ordinal() - rcloudState.ordinal();
        }else if(lcloudState == null){
            return 1;
        }else {
            return -1;
        }
    }

    private int compareUsed(boolean lused,boolean rused){
        if(lused == rused){
            return 0;
        }else if(lused == true && rused == false){
            return 1;
        }else {
            return -1;
        }
    }

    private int compareCloudInfo(UserCloudInfo lcloudInfo, UserCloudInfo rcloudInfo){
        if(lcloudInfo != null && rcloudInfo != null){
            long ret = lcloudInfo.getExpireTime() - rcloudInfo.getExpireTime();
            if(ret > 0) {
                return 1;
            } else if(ret < 0){
                return -1;
            }else{
                return 0;
            }
        }else if(lcloudInfo == null){
            return 1;
        }else {
            return -1;
        }
    }
}
